﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ExameRegister.Views;
using ExameRegister.Assert;
using System.ComponentModel;
using System.Collections.ObjectModel;
using ExameRegister.Model;

namespace ExameRegister
{
    public partial class MainPage : UserControl, IAsyncCtrl, IReturnListenerc, INotifyPropertyChanged
    {
        //private WCFService.WCFServiceClient client = null;
        ServiceUtility su = new ServiceUtility();
        private MainControl _parent = null;
        public MainPage(MainControl parent)
        {
            InitializeComponent();
            _parent = parent;
            //client = new WCFService.WCFServiceClient();
            //client.LoginCompleted += new EventHandler<WCFService.LoginCompletedEventArgs>(client_LoginCompleted);
            su.OnCompletedCallService += new EventHandler<WCFEvent>(su_OnCompletedCallService);
            su.CallService("ExameRegister.BO.LoginBO", "GetPublicOfficialInfo", new object[] { "Status=1" }, this, "GetPublicOfficialInfo");
        }




        void su_OnCompletedCallService(object sender, WCFEvent e)
        {
            string method = e.UserState as string;
            switch (method)
            {
                case "Login":
                    //bool result = Utility.ConvertFromJson<bool>(e.Result);
                    if (!string.IsNullOrEmpty(e.Result))
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(e.Result.TrimEnd('"').TrimStart('"'));
                    }
                    else
                    {
                        if (Sessions.Session.Keys.Contains("username"))
                            Sessions.Session.Remove("username");
                        if (Sessions.Session.Keys.Contains("idcard"))
                            Sessions.Session.Remove("idcard");
                        Sessions.Session.Add("username", tbName.Text);
                        Sessions.Session.Add("idcard", tbIDCard.Text);
                        if (PropertyChanged != null)
                            PropertyChanged(this, new PropertyChangedEventArgs("loginComplete"));
                        //InfomationControl view = new InfomationControl();
                        //view.Nav = this.Nav;
                        //ReturnAsyncCtrl page = new ReturnAsyncCtrl(view) { Nav = this.Nav, PrevPage = this, Title = tbName.Text };
                        //this.Nav.SetNextPage(page);
                        //this.Nav.MoveNext();
                    }
                    break;
                case "GetPublicOfficialInfo":
                    ObservableCollection<PublicOfficialInfo> infos = Utility.ConvertFromJson<ObservableCollection<PublicOfficialInfo>>(e.Result);
                    var entity = infos.FirstOrDefault();
                    if (entity != null)
                    {
                        tbTitle.Text = entity.Name;
                        tbOffice.Text = entity.Office;
                        if (entity.YM != null)
                            tbYM.Text = entity.YM.Value.Year + "年" + entity.YM.Value.Month + "月";
                    }
                    else
                    {
                        _parent.LayoutRoot.Children.Clear();
                        _parent.LayoutRoot.Children.Add(new ErrorControl(_parent.hlbExist));
                    }
                    break;
            }
        }

        //void client_LoginCompleted(object sender, WCFService.LoginCompletedEventArgs e)
        //{
        //    if (e.Result == false)
        //    {
        //        SilverlightMessageBoxLibrary.Message.ErrorMessage("姓名或密码错误!");
        //    }
        //    else
        //    {
        //        if (Sessions.Session.Keys.Contains("username"))
        //            Sessions.Session.Remove("username");
        //        Sessions.Session.Add("username", tbName.Text);
        //        InfomationControl view = new InfomationControl();
        //        view.Nav = this.Nav;
        //        ReturnAsyncCtrl page = new ReturnAsyncCtrl(view) { Nav = this.Nav, PrevPage = this, Title = tbName.Text };
        //        this.Nav.SetNextPage(page);
        //        this.Nav.MoveNext();
        //    }
        //}

        /// <summary>
        /// 重置按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            tbIDCard.Text = "";
            tbName.Text = "";
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string name = tbName.Text;
            string idcard = tbIDCard.Text;
            
            if (!string.IsNullOrEmpty(name))
            {
                if (Sessions.ADMIN.Equals(name))
                {
                    su_OnCompletedCallService(null, new WCFEvent("") { UserState = "Login" });
                    return;
                }
                su.CallService("ExameRegister.BO.LoginBO", "Login", new object[] { name, idcard }, this, "Login");
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("姓名或证件号码不能为空!");
            }
        }

        public string Title
        {
            get
            ;
            set
            ;
        }

        public bool PageLoaded
        {
            get
           ;
            set
            ;
        }

        public NavMoveSwitch Nav
        {
            get
            ;
            set
            ;
        }

        public void BeginReturn()
        {
            return;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Button_Click_1(null, null);
            }
        }
    }
}
